package leetcode_day._2022._202203._1120;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/3/14 12:49
 * 两个列表的最小索引总和
 * 算法：哈希表
 */
public class _14_599 {

    public String[] findRestaurant(String[] list1, String[] list2) {
        Map<String, Integer> map = new HashMap<>(list1.length);
        for (int i = 0; i < list1.length; i++) map.put(list1[i], i);
        List<String> ans = new ArrayList<>();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < list2.length; i++) {
            if (map.containsKey(list2[i])) {
                int tmp = map.get(list2[i]) + i;
                if (tmp == min) ans.add(list2[i]);
                else if (tmp < min){
                    min = tmp;
                    ans.clear();
                    ans.add(list2[i]);
                }
            }
        }
        return ans.toArray(new String[0]);
    }

}
